﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using SysManage.BLL;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Edu;
using ZHDJ.Data.Models.Questionnaire;
using ZHDJ.WebPage;

namespace SysManage.Module.Questionnaire
{
    public partial class PartyQuestionnaireSubjectEdit : PageBase
    {
        public string id { get { return GetQueryIntValue("id"); } }
        public string QuestionnaireId { get { return GetQueryIntValue("QuestionnaireId"); } }
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "PartyQuestionnaireEdit";
            }
        }

        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();


            IQueryable<PartyQuestionnaireSubject> q = DB.PartyQuestionnaireSubjects;
     
 
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyQuestionnaireSubject current = DB.PartyQuestionnaireSubjects.Find(id);
                if (current == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                this.txtTitle.Text = current.Title;
                ddlCategory.SelectedValue = current.Category;
                txtOrderNo.Text = current.OrderNo.ToString();
                cbxEnabled.Checked = current.Enabled;
                var op = DB.PartyQuestionnaireOptions.Where(a => a.SubjectID == current.ID).OrderBy(a => a.OrderNo);
                var index = 1;
                foreach (var item in op)
                {
                    var text = FineUI.ControlUtil.FindControl("TextBox" + index) as FineUI.TextBox;
                    var radio = FineUI.ControlUtil.FindControl("RadioButtonList" + index) as FineUI.RadioButtonList;
                    if (text == null || radio == null)
                    {
                        continue;
                    }
                    text.Text = item.Title;
                    radio.SelectedValue = item.IsTk ? "1" : "0";
                    index += 1;
                }
            }
        }

        #endregion

        #region Events

        private void SaveJobTitle()
        {
            //删除选项
            var subjectid = "";
            string id = GetQueryIntValue("id");
            subjectid = id;
            PartyQuestionnaireSubject item;
            if (id != "")
            {
                //更新
                item = DB.PartyQuestionnaireSubjects.Find(id);
                if (item == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                item.ModifyTime = DateTime.Now;
                item.ModifyBy = CurrentUser.ID;
               
                //移除选项
                //DB.PartyEduSubjectOptions.Where(u => u.SubjectID == item.ID).ToList().ForEach(u => DB.PartyEduSubjectOptions.Remove(u));
            }
            else
            {
                item = new PartyQuestionnaireSubject();
                subjectid=item.ID = StringHelper.NewID;
                item.QuestionnaireID = QuestionnaireId;
                item.CreateTime = DateTime.Now;
                item.CreateBy = CurrentUser.ID;
                item.ModifyTime = DateTime.Now;
                item.ModifyBy = CurrentUser.ID;
                DB.PartyQuestionnaireSubjects.Add(item);

            }
            item.OrderNo = Convert.ToInt32(txtOrderNo.Text);
            item.Title = txtTitle.Text;
            item.Category = ddlCategory.SelectedValue;
            item.Enabled = cbxEnabled.Checked;
            //DB.SaveChanges();
            var ops = DB.PartyQuestionnaireOptions.Where(a => a.SubjectID == item.ID).OrderBy(a => a.OrderNo).ToList();
            //保存选项
            for (int i = 1; i < 9; i++)
            {
                var text = FineUI.ControlUtil.FindControl("TextBox" + i) as FineUI.TextBox;
                var radio = FineUI.ControlUtil.FindControl("RadioButtonList" + i) as FineUI.RadioButtonList;
                if (text == null || radio == null)
                {
                    continue;
                }
                if (radio.SelectedValue == "" || text.Text.Trim() == "")
                {
                    continue;
                }
                PartyQuestionnaireOption op = new PartyQuestionnaireOption();
                if (ops.Count() >= i)
                {
                    op = DB.PartyQuestionnaireOptions.Find(ops[i-1].ID);
                    item.ModifyTime = DateTime.Now;
                    item.ModifyBy = CurrentUser.ID;
                }
                else
                {
                    op.ID = StringHelper.NewID;
                    item.CreateTime = DateTime.Now;
                    item.CreateBy = CurrentUser.ID;
                    item.ModifyTime = DateTime.Now;
                    item.ModifyBy = CurrentUser.ID;
                    DB.PartyQuestionnaireOptions.Add(op);
                }
               
                op.IsTk = radio.SelectedValue == "1";
                op.Title = text.Text;
                op.OrderNo = i;
                op.SubjectID = subjectid;
            }
            DB.SaveChanges();
        }



        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveJobTitle();

            //Alert.Show("添加成功！", String.Empty, ActiveWindow.GetHidePostBackReference());
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion

    }
}
